package co.fitstart.admin.web.controller.user.experience;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.search.OnlineExperienceSignUpSearch;
import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.ExperienceSignUpService;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.market.OnlineExperienceSignUp;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.utils.DateFormatUtils;

/**
 * 线上体验课报名
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("online-experience-sign-ups")
@MenuSetting("menu-user")
public class OnlineExperienceSignUpController {

    private static final Logger logger = LoggerFactory.getLogger(OfflineExperienceSignUpController.class);

    @Autowired
    private PrincipalService principalService;

    @Autowired
    private ExperienceSignUpService experienceSignUpService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-online-experience-sign-up-management")
    public ModelAndView list(@RequestParam(value = "handled", required = false) Boolean handled,
                             Page page,
                             HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("user/experience/online-sign-up-list");
        
        List<OnlineExperienceSignUp> experiences = experienceSignUpService.listOnline(handled, page);
        
        modelAndView.addObject("handled", handled);
        modelAndView.addObject("experiences", experiences);
        
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }

    /**
     * 
     * @param experienceId
     * @return
     */
    @RequestMapping(value = "/{experienceId}/handle", method = RequestMethod.GET)
    @MenuSetting("submenu-online-experience-sign-up-management")
    public ModelAndView handle(@PathVariable("experienceId") Long experienceId) {
        ModelAndView modelAndView = new ModelAndView("user/experience/online-sign-up-handle");
        
        modelAndView.addObject("experienceId", experienceId);
        
        return modelAndView;
    }

    /**
     * 
     * @param login
     * @param experienceId
     * @param remark
     * @return
     */
    @RequestMapping(value = "/{experienceId}/handle", method = RequestMethod.PATCH)
    public @ResponseBody String onHandle(@Secure Login login,
                                         @PathVariable("experienceId") Long experienceId,
                                         @RequestParam("remark") String remark){
        
        Result result = new Result();
        
        try{
            Principal handler = principalService.load(Principal.class, login.getId());
            OnlineExperienceSignUp experience = experienceSignUpService.loadOnline(experienceId);
            
            experienceSignUpService.handle(experience, remark, handler);
            
            result.success().message("提交成功");
            
        } catch(Exception e){
            
            logger.error("#OnlineExperienceSignUpController# onHandle() error: ", e);
            
            result.fail().message("提交失败");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }

    /**
     * 
     * @param siteId
     * @param search
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-online-experience-sign-up-management")
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("user/experience/online-sign-up-search");
        
        modelAndView.addObject("today", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        
        return modelAndView;
    }

    /**
     * 
     * @param login
     * @param siteId
     * @param search
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-online-experience-sign-up-management")
    public ModelAndView onSearch(OnlineExperienceSignUpSearch search,
                                 Page page,
                                 HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("user/experience/online-sign-up-list");
        
        List<OnlineExperienceSignUp> experiences = experienceSignUpService.search(search, page);
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        modelAndView.addObject("experiences", experiences);
        
        return modelAndView;
    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    @MenuSetting("submenu-online-experience-sign-up-management")
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("user/experience/online-sign-up-export");
        
        modelAndView.addObject("today", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        
        return modelAndView;
    }

    /**
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(OnlineExperienceSignUpSearch search) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("online-experience-sign-up-report.xls"));
        
        List<OnlineExperienceSignUp> experiences = experienceSignUpService.search(search);
        
        modelAndView.addObject("experiences", experiences);
        modelAndView.addObject("beginDate", search.getBegin() == null ? null : DateFormatUtils.format(search.getBegin(), "yyyy-MM-dd"));
        modelAndView.addObject("endDate", search.getEnd() == null ? null : DateFormatUtils.format(search.getEnd(), "yyyy-MM-dd"));
        
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
    }
}
